package com.shuqi.activity.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.shuqi.common.utils.StringUtils;
import com.shuqi.controller.R;
import com.shuqi.model.bean.ItemInfo;

/**
 * 充值记录适配器
 * 
 * @author zhuchen
 * 
 */
public class RechargesAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<ItemInfo> list;

    private Context context;

    public RechargesAdapter(Context context) {
        this.inflater = LayoutInflater.from(context);
        this.context = context;
    }

    public RechargesAdapter(Context context, List<ItemInfo> list) {
        this.inflater = LayoutInflater.from(context);
        this.list = list;
    }

    public void setList(List<ItemInfo> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        if (list != null && list.get(position) != null) {
            return list.get(position);
        } else {
            return null;
        }
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.item_recharges_list, null);
        }
        Holder holder = (Holder) view.getTag();
        if (holder == null) {
            holder = new Holder();
            holder.icon_recharges = (ImageView) view.findViewById(R.id.icon_recharges);
            holder.item_recharges_money = (TextView) view.findViewById(R.id.item_recharges_money);
            holder.item_recharges_date = (TextView) view.findViewById(R.id.item_recharges_date);
            holder.item_recharges_status = (TextView) view.findViewById(R.id.item_recharges_status);
            view.setTag(holder);
        }
        ItemInfo info = list.get(position);
        if (StringUtils.isNotBlank(info.getTypeId())) {
            try {
                int typeId = Integer.parseInt(info.getTypeId());
                int imageId = R.drawable.icon_p_alipay;
                switch (typeId) {
                    case 1:
                        imageId = R.drawable.icon_p_yidong;
                        break;
                    case 2:
                        imageId = R.drawable.icon_p_liantong;
                        break;
                    case 3:
                        imageId = R.drawable.icon_p_dianxin;
                        break;
                    case 4:
                        imageId = R.drawable.icon_p_junka;
                        break;
                    case 5:
                        imageId = R.drawable.icon_p_shenda;
                        break;
                    case 6:
                        imageId = R.drawable.icon_p_zhengtu;
                        break;
                    case 7:
                        imageId = R.drawable.icon_p_wanmei;
                        break;
                    case 8:
                        imageId = R.drawable.icon_p_alipay;
                        break;
                    default:
                        break;
                }
                holder.icon_recharges.setBackgroundDrawable(context.getResources().getDrawable(
                        imageId));

            } catch (Exception e) {
                Log.d("RechargesAdapter", "", e);
            }

        }

        // 还有7种
        holder.item_recharges_money.setText("充值金额 : " + info.getAmount() + "元");
        holder.item_recharges_date.setText(times(Long.parseLong(info.getDate())));
        if (info.getCode().equals("1")) {
            holder.item_recharges_status.setText(info.getStatus());
            holder.item_recharges_status.setTextColor(Color.parseColor("#14be7d"));
        } else if (info.getCode().equals("2")) {
            holder.item_recharges_status.setText(info.getStatus());
            holder.item_recharges_status.setTextColor(Color.parseColor("#9fa0a0"));
        } else if (info.getCode().equals("3")) {
            holder.item_recharges_status.setText(info.getStatus());
            holder.item_recharges_status.setTextColor(Color.parseColor("#ff361b"));
        }
        return view;
    }

    private static class Holder {

        /** 充值方式图标 */
        public ImageView icon_recharges;
        /** 支付金额 */
        public TextView item_recharges_money;
        /** 支付时间 */
        public TextView item_recharges_date;
        /** 支付状态 */
        public TextView item_recharges_status;
    }

    private String times(long time) {
        // 自定义格式
        SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
        Date currentTime = new Date(time);
        return format.format(currentTime);
    }

}
